package com.macro.mall.portal.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.api.client.util.Base64;
import com.macro.mall.portal.service.WxService;
import com.macro.mall.portal.util.HttpKit;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Service
public class WxServiceImpl implements WxService {
    @Autowired
    private Environment env;
    @Override
    public String accessToken() {
        RestTemplate restTemplate =new RestTemplate();
        String url = String.format(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
                env.getProperty("wx.appid"), env.getProperty("wx.secret"));
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        String body = responseEntity.getBody();
//        System.out.println(body.toString());
        Map<String, Object> resMap = JSON.parseObject(body,Map.class);
        String token = resMap.get("access_token").toString();
        System.out.println(token);
        return token;
    }

    @Override
    public String getLiveInfo(Integer start, Integer limit) {
        String url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token="+accessToken();
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String,Object> map = new HashMap<>();
        map.put("start",start);
        map.put("limit",limit);
        String data = JSONObject.fromObject(map).toString();
        String res1 = HttpKit.post(url,data,headers);
        return res1;
    }

//    @Override
//    public String getWxAcodeunLimit(String scene, String page) {
//        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken();
//        Map<String, String> headers = new HashMap<>();
//        headers.put("Content-Type", "application/json");
//        Map<String,Object> map = new HashMap<>();
//        map.put("scene",scene);
//        map.put("page",page);
//        String data = JSONObject.fromObject(map).toString();
//        String res1 = HttpKit.post(url,data,headers);
//        System.out.println(res1);
//        return res1;
//    }
    public String getWxAcodeunLimit(String sceneStr, String page) {
        RestTemplate rest = new RestTemplate();
        String img=null;
        try {
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken();
            Map<String,Object> param = new HashMap<>();
            param.put("scene", sceneStr);
            param.put("page", page);
            param.put("width", 430);
            param.put("auto_color", false);
            Map<String,Object> line_color = new HashMap<>();
            line_color.put("r", 0);
            line_color.put("g", 0);
            line_color.put("b", 0);
            param.put("line_color", line_color);
            //LOG.info("调用生成微信URL接口传参:" + param);
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            HttpEntity requestEntity = new HttpEntity(param, headers);
            ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
            //LOG.info("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());
            byte[] result = entity.getBody();
            //LOG.info(Base64.encodeBase64String(result));
            img =Base64.encodeBase64String(result);
            //inputStream = new ByteArrayInputStream(result);

           // File file = new File("C:/Users/wangqiulin/Desktop/1.png");
           // if (!file.exists()){
           //     file.createNewFile();
           // }
           // outputStream = new FileOutputStream(file);
           // int len = 0;
          // byte[] buf = new byte[1024];
          //  while ((len = inputStream.read(buf, 0, 1024)) != -1) {
           //     outputStream.write(buf, 0, len);
          //  }
          //  outputStream.flush();
        } catch (Exception e) {
            //LOG.error("调用小程序生成微信永久小程序码URL接口异常",e);
        }
        return img;
    }
}
